package com.example.app1;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.net.URI;
import java.sql.SQLTransactionRollbackException;

public class Intent8Activity extends AppCompatActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent8);

        Button btn1 = findViewById(R.id.btn1);
        Button btn2 = findViewById(R.id.btn2);
        Button btn3 = findViewById(R.id.btn3);
        Button btn4 =  findViewById(R.id.btn4);
        Button btn5 = findViewById(R.id.btn5);
        Button btn6 = findViewById(R.id.btn6);
        Button btn7 =  findViewById(R.id.btn7);
        Button btn8 = findViewById(R.id.btn8);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.R)
    @Override
    public void onClick(View v) {
        String num = "10000";
        Intent intent;
        switch (v.getId()){
            case R.id.btn1:
                intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://www.baidu.com"));
                startActivity(intent);
                break;
            case R.id.btn2:
                intent = new Intent(Intent.ACTION_CALL_BUTTON);
                startActivity(intent);
                break;
            case R.id.btn3:
                //在AndroidManifest中加上权限  下面这段代码申请权限 Android高版本单纯fest加没用，需要动态申请权限
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1);
                } else {
                    intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:" + num));
                    startActivity(intent);
                }
                break;
            case R.id.btn4:
                intent = new Intent(Intent.ACTION_VIEW);
                intent.setType("vnd.android-dir/mms-sms");
                startActivity(intent);
                break;
            case R.id.btn5:
                intent = new Intent(Intent.ACTION_SENDTO,
                        Uri.parse("smsto:"+num));
                intent.putExtra("sms_body","1111111");
                startActivity(intent);
                break;
            case R.id.btn6:
                //获取存储权限
                if (Environment.isExternalStorageManager()) {
                    intent = new Intent(Intent.ACTION_VIEW);
                    Uri uri= Uri.parse("https://dl.qianicloud.cn/typora/星辰 - 雷雨季节-星辰.mp3");
                    intent.setDataAndType(uri,"audio/*");
                    startActivity(intent);
                }else{
                    Intent per = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                    startActivity(per);
                }
                break;
            case R.id.btn7:
                intent=new Intent(Intent.ACTION_DELETE);
                Uri uri=Uri.parse("package:com.example.app1");
                intent.setData(uri);
                startActivity(intent);
                break;
            case R.id.btn8:
                if (Environment.isExternalStorageManager()) {
                   intent = new Intent(Intent.ACTION_VIEW);
                    String path = "/storage/emulated/0/Download/app-release.apk";
                    Uri uri1 = FileProvider.getUriForFile(Intent8Activity.this,"com.example.app1.fileProvider",new File(path));
                    intent.setDataAndType(uri1, "application/vnd.android.package-archive");
//                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);/
                    startActivity(intent);
                }else{
                    Intent per = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
                    startActivity(per);
                }

                break;
        }

    }




}